import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectCity {
    public static void main(String[] args) throws SQLException {
        //1.获得数据源
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?characterEncoding=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("12345678");
        //2.获得连接
        Connection connection= (Connection) dataSource.getConnection();

        //3.1组装sql 获得执行器
        String selectSql="select * from city where id<?";
        PreparedStatement statement=connection.prepareStatement(selectSql);
        //3.2填充占位符
        statement.setInt(1,4);
        //4/执行sql
        ResultSet resultSet=statement.executeQuery();
        while (resultSet.next()){
            City city=new City();
            city.setId(resultSet.getInt("id"));
            city.setName(resultSet.getString("name"));
            System.out.println(city);
        }
        //5.关闭资源
        resultSet.close();
        statement.close();
        connection.close();

    }
}
